#pragma once

#include <vector>
#include <unordered_map>

#include "noncopyable.h"
#include "Timestamp.h"

class Channel;
class EventLoop;

// muduo库中多路事件分发器的核心IO复用模块
class Poller : public noncopyable
{
public:
    using ChannelList = std::vector<Channel*>;

    Poller(EventLoop* loop);
    virtual ~Poller() = default;

    // 定义统一的接口，让子类进行重写
    virtual Timestamp poll(int timeoutMs, ChannelList* activeChannels) = 0;
    // 更新Channel所感兴趣的事件
    virtual void updateChannel(Channel* channel) = 0;
    // 将Channel从EventLoop中移除
    virtual void removeChannel(Channel* channel) = 0;

    // 判断Channel是否在当前Poller当中
    bool hasChannel(Channel *channel) const;

    // EventLoop可以通过该接口获取默认的IO复用的具体实现
    static Poller* newDefaultPoller(EventLoop* loop);

protected:
    // 定义fd与fd所属的Channel的哈希表
    using ChannelMap = std::unordered_map<int, Channel*>;
    ChannelMap channels_;

private:
    EventLoop* ownerLoop_; // 定义Poller所属的事件循环EventLoop
};